package com.plian.system.service.hr.hrOld.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.plian.Tools.*;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.constant.baseinfo.ValueSetConstant;
import com.plian.system.dto.hr.staff.StaffFilesDTO;
import com.plian.system.entity.hr.staffAnnualBackup.*;
import com.plian.system.enumeratea.PartTimeStatus;
import com.plian.system.mapper.hr.hrbackup.*;
import com.plian.system.model.CommonCode;
import com.plian.system.model.R;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.bt.baseinfo.BaseInfoService;
import com.plian.system.service.hr.dept.IDeptService;
import com.plian.system.service.hr.hrOld.IStaffFilesOldService;
import com.plian.system.service.hr.recruitment.IPartTimeInfoService;
import com.plian.system.service.sys.orgzation.OrgzationService;
import com.plian.system.vo.hr.staff.StaffFilesVO;
import com.plian.system.vo.hr.staffAnnualBackup.StaffFilesOldVO;
import com.plian.system.vo.report.HeaderVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.report.ReportVO;
import com.plian.system.wrapper.hr.hrbackup.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static com.plian.system.constant.sys.BaseConstant.BASE64_KEY;

/**
 * 员工档案 服务实现类
 *
 * @author gangan
 * @since 2019-09-29
 */
@Service
@AllArgsConstructor
public class StaffFilesOldServiceImpl extends BaseServiceImpl<StaffFilesOldMapper, StaffFilesOld> implements IStaffFilesOldService {


    private IPartTimeInfoService partTimeInfoService;
    @Resource
    private StaffFilesOldMapper staffFilesOldMapper;
    @Resource
    private AnnualAssessmentOldMapper annualAssessmentOldMapper;
    @Resource
    private AppointRemoveOldMapper appointRemoveOldMapper;
    @Resource
    private EducationDegreeOldMapper educationDegreeOldMapper;
    @Resource
    private OverseasMembersOldMapper overseasMembersOldMapper;
    @Resource
    private PractitionerCertificateOldMapper practitionerCertificateOldMapper;
    @Resource
    private RewardsPunishmentsOldMapper rewardsPunishmentsOldMapper;
    @Resource
    private SocialRelationsOldMapper socialRelationsOldMapper;
    @Resource
    private TechnicalQualificationOldMapper technicalQualificationOldMapper;
    @Resource
    private TrainOldMapper trainOldMapper;
    @Resource
    private WorkExperienceOldMapper workExperienceOldMapper;
    private StaffFilesOldWrapper staffFilesOldWrapper;




    @Resource
    private AnnualAssessmentOldWrapper annualAssessmentOldWrapper;
    @Resource
    private AppointRemoveOldWrapper appointRemoveOldWrapper;
    @Resource
    private EducationDegreeOldWrapper educationDegreeOldWrapper;
    @Resource
    private OverseasMembersOldWrapper overseasMembersOldWrapper;
    @Resource
    private PractitionerCertificateOldWrapper practitionerCertificateOldWrapper;
    @Resource
    private RewardsPunishmentsOldWrapper rewardsPunishmentsOldWrapper;
    @Resource
    private SocialRelationsOldWrapper socialRelationsOldWrapper;
    @Resource
    private TechnicalQualificationOldWrapper technicalQualificationOldWrapper;
    @Resource
    private TrainOldWrapper trainOldWrapper;
    @Resource
    private WorkExperienceOldWrapper workExperienceOldWrapper;
    private static Map<String, String> allCodeMap = new HashMap<>();
    private BaseInfoService baseInfoService;
    private IDeptService deptService;
    private static Map<Long, String> deptMap = new HashMap<>();
    private OrgzationService orgzationService;
    private static Map<String, String> orgMap = new HashMap<>();
    @Override
    public R<StaffFilesOldVO> getDetailObj(StaffFilesOldVO staffFilesOldVO) {
        if (!Optional.ofNullable(staffFilesOldVO).isPresent()) {
            return new R(CommonCode.FAIL);
        }
        staffFilesOldVO.setPartTimeJobSituation(partTimeInfoService.getPartTimeJobSituation(getByDocumentNumber(staffFilesOldVO.getDocumentNumber(),staffFilesOldVO.getBackYear())));

        staffFilesOldVO.setPartTimeInfos(partTimeInfoService.getPartTimeInfosByStatus(staffFilesOldVO.getId(), PartTimeStatus.INCUMBENCY.getStatus()));
        setChild(staffFilesOldVO);

        R<StaffFilesOldVO> objResult = new R<>(CommonCode.SUCCESS);
        objResult.setPageData(staffFilesOldVO);
        return objResult;
    }
    private List<Long> getByDocumentNumber(String documentNumber,String backYear) {
        LambdaQueryWrapper<StaffFilesOld> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaffFilesOld::getDocumentNumber, documentNumber);
        queryWrapper.eq(StaffFilesOld::getBackYear, backYear);
        List<StaffFilesOld> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.stream().map(StaffFilesOld::getId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
    @Override
    public StaffFilesOldVO setChild(StaffFilesOldVO staffFilesOldVO) {
        if (!Optional.ofNullable(staffFilesOldVO).isPresent()) {
            return null;
        }
        staffFilesOldVO.setAppointRemoveVO(appointRemoveOldWrapper.entityToVO(appointRemoveOldMapper
                .selectList(new LambdaQueryWrapper<AppointRemoveOld>().eq(AppointRemoveOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setWorkExperienceVO(workExperienceOldWrapper.entityToVO(workExperienceOldMapper
                .selectList(new LambdaQueryWrapper<WorkExperienceOld>().eq(WorkExperienceOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setRewardsPunishmentsVO(rewardsPunishmentsOldWrapper.entityToVO(rewardsPunishmentsOldMapper
                .selectList(new LambdaQueryWrapper<RewardsPunishmentsOld>().eq(RewardsPunishmentsOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setAnnualAssessmentVO(annualAssessmentOldWrapper.entityToVO(annualAssessmentOldMapper
                .selectList(new LambdaQueryWrapper<AnnualAssessmentOld>().eq(AnnualAssessmentOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setEducationDegreeVO(educationDegreeOldWrapper.entityToVO((educationDegreeOldMapper
                .selectList(new LambdaQueryWrapper<EducationDegreeOld>().eq(EducationDegreeOld::getStaffFilesId, staffFilesOldVO.getId())))));
        staffFilesOldVO.setSocialRelationsVO(socialRelationsOldWrapper.entityToVO(socialRelationsOldMapper
                .selectList(new LambdaQueryWrapper<SocialRelationsOld>().eq(SocialRelationsOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setPractitionerCertificateVO(practitionerCertificateOldWrapper.entityToVO(practitionerCertificateOldMapper
                .selectList(new LambdaQueryWrapper<PractitionerCertificateOld>().eq(PractitionerCertificateOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setTechnicalQualificationVO(technicalQualificationOldWrapper.entityToVO(technicalQualificationOldMapper
                .selectList(new LambdaQueryWrapper<TechnicalQualificationOld>().eq(TechnicalQualificationOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setTrainVO(trainOldWrapper.entityToVO(trainOldMapper
                .selectList(new LambdaQueryWrapper<TrainOld>().eq(TrainOld::getStaffFilesId, staffFilesOldVO.getId()))));
        staffFilesOldVO.setOverseasMembersVO(overseasMembersOldWrapper.entityToVO(overseasMembersOldMapper
                .selectList(new LambdaQueryWrapper<OverseasMembersOld>().eq(OverseasMembersOld::getStaffFilesId, staffFilesOldVO.getId()))));
        return staffFilesOldVO;
    }
    @Override
    public ReportReturnVO reportToYear(StaffFilesDTO staffFilesDTO) {
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        ReportVO reportVO = new ReportVO();

        reportVO
                .addHeader(HeaderVO.builder().key("companyName").title("单位").build())
                .addHeader(HeaderVO.builder().key("staffName").title("姓名").build())
                .addHeader(HeaderVO.builder().key("jobNameId").title("职务").build())
                .addHeader(HeaderVO.builder().key("genderName").title("性别").build())
                .addHeader(HeaderVO.builder().key("birthday").title("出生日期").build())
                .addHeader(HeaderVO.builder().key("nationName").title("民族").build())
                .addHeader(HeaderVO.builder().key("politicCountenanceName").title("政治面貌").build())
                .addHeader(HeaderVO.builder().key("staffCategoryName").title("人员类别").build())
                .addHeader(HeaderVO.builder().key("talentCategory").title("人才类别").build())
                .addHeader(HeaderVO.builder().key("nativePlace").title("籍贯").build())
                .addHeader(HeaderVO.builder().key("birthPlace").title("出生地").build())
                .addHeader(HeaderVO.builder().key("workTime").title("参加工作时间").build())
                .addHeader(HeaderVO.builder().key("technicalPosition").title("专业技术职称").build())
                .addHeader(HeaderVO.builder().key("educationalName").title("最高学历").build())
                .addHeader(HeaderVO.builder().key("degreeName").title("最高学位").build())
                .addHeader(HeaderVO.builder().key("staffCode").title("身份证号").build())
        ;

        List<StaffFilesOld> staffFiles = staffFilesOldMapper.reportDataPage(staffFilesDTO.getCompanyNameIds(), staffFilesDTO.getStaffName(), staffFilesDTO.getGenderIds(), staffFilesDTO.getJobNameId(),
                staffFilesDTO.getEducationalIds(), staffFilesDTO.getTechnicalPosition(), staffFilesDTO.getCompanyInitiationTimeStart(), staffFilesDTO.getCompanyInitiationTimeEnd(),
                staffFilesDTO.getWorkTimeStart(), staffFilesDTO.getWorkTimeEnd(), staffFilesDTO.getPoliticCountenanceIds(),
                staffFilesDTO.getIsReserveCadre(), staffFilesDTO.getIsCorporateTalent(), staffFilesDTO.getManagementCategoryIds(),staffFilesDTO.getYear());
        if (CollectionUtil.isNotEmpty(staffFiles)) {

            List<StaffFilesOldVO> list = setOldVO(staffFiles);
//			List<StaffFilesVO> list = staffFilesWrapper.entityToVO(staffFiles);

            //给所有数据按照单位编码升序，人员编码升序
            list = list.parallelStream().filter(staffFilesOldVO -> StringUtil.isNotBlank(staffFilesOldVO.getCompanyCode()))
                    .sorted(Comparator.comparing(StaffFilesOldVO::getCompanyCode)).collect(Collectors.toList());
            List<StaffFilesOldVO> list2 = new ArrayList<>();
            String companyNameSearchId = "";
            int count = 0;
            int sum = 0;
            for (int i = 0; i < list.size(); i++) {
                if (!companyNameSearchId.equals(list.get(i).getCompanyNameId())) {
                    if (i > 0) {
                        StaffFilesOldVO Total = new StaffFilesOldVO();
                        Total.setCompanyName("小计：" + count);
                        list2.add(Total);
                        sum += count;
                    }
                    companyNameSearchId = list.get(i).getCompanyNameId();
                    count = 1;
                } else {
                    count++;
                }
                list2.add(list.get(i));
            }
            sum += count;
            StaffFilesOldVO LastTotal = new StaffFilesOldVO();
            LastTotal.setCompanyName("小计：" + count);
            list2.add(LastTotal);

            StaffFilesOldVO SumTotal = new StaffFilesOldVO();
            SumTotal.setCompanyName("总计：" + sum);
            list2.add(SumTotal);

            reportVO.setData(list2);
        }

        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }
    public List<StaffFilesOldVO> setOldVO(List<StaffFilesOld> staffFilesList) {
        if (CollectionUtil.isEmpty(staffFilesList)) {
            return new ArrayList<>();
        }
        allCodeMap = baseInfoService.getChildMap(ValueSetConstant.ROOT);
        deptService.list().parallelStream().forEach(dept -> deptMap.put(dept.getId(), dept.getDeptName()));
        List<PageData> pageDataList = (List<PageData>) orgzationService.listAll().getPageData().get("list");
        pageDataList.parallelStream().forEach(pd -> orgMap.put((String) pd.get("id"), pd.get("name") + StringPool.DASH + pd.get("orgCode")));
        List<StaffFilesOldVO> staffFilesOldVOS = new ArrayList<>();
        for (StaffFilesOld staffFiles : staffFilesList) {
            staffFilesOldVOS.add(entityToOldVo(staffFiles));
        }
        return staffFilesOldVOS;
    }
    private StaffFilesOldVO entityToOldVo(StaffFilesOld staffFiles) {
        StaffFilesOldVO staffFilesVO = new StaffFilesOldVO();
        BeanUtils.copyProperties(staffFiles, staffFilesVO);
        if (StringUtil.isNotBlank(staffFiles.getStaffCode())) {
            staffFilesVO.setStaffCode(EncryptUtil.decryptBase64(staffFiles.getStaffCode(), BASE64_KEY));
        }
        if (StringUtil.isNotBlank(staffFiles.getDocumentNumber())) {
            staffFilesVO.setDocumentNumber(EncryptUtil.decryptBase64(staffFiles.getDocumentNumber(), BASE64_KEY));
        }
        if (StringUtil.isNotBlank(staffFilesVO.getDocumentTypeId()) && allCodeMap.containsKey(staffFilesVO.getDocumentTypeId())) {
            staffFilesVO.setDocumentTypeName(allCodeMap.get(staffFilesVO.getDocumentTypeId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getGenderId()) && allCodeMap.containsKey(staffFilesVO.getGenderId())) {
            staffFilesVO.setGenderName(allCodeMap.get(staffFilesVO.getGenderId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getNationId()) && allCodeMap.containsKey(staffFilesVO.getNationId())) {
            staffFilesVO.setNationName(allCodeMap.get(staffFilesVO.getNationId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getPoliticCountenanceId()) && allCodeMap.containsKey(staffFilesVO.getPoliticCountenanceId())) {
            staffFilesVO.setPoliticCountenanceName(allCodeMap.get(staffFilesVO.getPoliticCountenanceId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getNationalityId()) && allCodeMap.containsKey(staffFilesVO.getNationalityId())) {
            staffFilesVO.setNationalityName(allCodeMap.get(staffFilesVO.getNationalityId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getMaritalStatusId()) && allCodeMap.containsKey(staffFilesVO.getMaritalStatusId())) {
            staffFilesVO.setMaritalStatusName(allCodeMap.get(staffFilesVO.getMaritalStatusId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getEducationalId()) && allCodeMap.containsKey(staffFilesVO.getEducationalId())) {
            staffFilesVO.setEducationalName(allCodeMap.get(staffFilesVO.getEducationalId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getDegreeId()) && allCodeMap.containsKey(staffFilesVO.getDegreeId())) {
            staffFilesVO.setDegreeName(allCodeMap.get(staffFilesVO.getDegreeId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getManagementCategoryId()) && allCodeMap.containsKey(staffFilesVO.getManagementCategoryId())) {
            staffFilesVO.setManagementCategoryName(allCodeMap.get(staffFilesVO.getManagementCategoryId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getCompilationTypeId()) && allCodeMap.containsKey(staffFilesVO.getCompilationTypeId())) {
            staffFilesVO.setCompilationTypeName(allCodeMap.get(staffFilesVO.getCompilationTypeId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getJobCategoryId()) && allCodeMap.containsKey(staffFilesVO.getJobCategoryId())) {
            staffFilesVO.setJobCategoryName(allCodeMap.get(staffFilesVO.getJobCategoryId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getRankId()) && allCodeMap.containsKey(staffFilesVO.getRankId())) {
            staffFilesVO.setRankName(allCodeMap.get(staffFilesVO.getRankId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getChangeEventsId()) && allCodeMap.containsKey(staffFilesVO.getChangeEventsId())) {
            staffFilesVO.setChangeEventsName(allCodeMap.get(staffFilesVO.getChangeEventsId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getChangeTypeId()) && allCodeMap.containsKey(staffFilesVO.getChangeTypeId())) {
            staffFilesVO.setChangeTypeName(allCodeMap.get(staffFilesVO.getChangeTypeId()));
        }

        if (StringUtil.isNotBlank(staffFilesVO.getHealthConditionId()) && allCodeMap.containsKey(staffFilesVO.getHealthConditionId())) {
            staffFilesVO.setHealthConditionName(allCodeMap.get(staffFilesVO.getHealthConditionId()));
        }

        if (staffFilesVO.getDepartmentNameId() != null) {
            if (deptMap.containsKey(Long.valueOf(staffFilesVO.getDepartmentNameId()))) {
                staffFilesVO.setDepartmentName(deptMap.get(Long.valueOf(staffFilesVO.getDepartmentNameId())));
            }
        }
        if (StringUtil.isNotBlank(staffFilesVO.getCompanyNameId())) {
            if (orgMap.containsKey(staffFilesVO.getCompanyNameId())) {
                String[] strs = orgMap.get(staffFilesVO.getCompanyNameId()).split(StringPool.DASH);

                staffFilesVO.setCompanyName(strs[0]);
                staffFilesVO.setCompanyCode(strs[1]);
            }
        }
        if (StringUtil.isBlank(staffFilesVO.getJobNameId())) {
            staffFilesVO.setJobNameId("");
        }
        staffFilesVO.setJobNameName(staffFilesVO.getJobNameId());

        if (staffFilesVO.getBirthday() != null) {
            staffFilesVO.setBirthdayStr(DateUtil.format(staffFilesVO.getBirthday(), DateUtil.PATTERN_DATE));
        }

        if (staffFilesVO.getJoinPartyTime() != null) {
            staffFilesVO.setJoinPartyTimeStr(DateUtil.format(staffFilesVO.getJoinPartyTime(), DateUtil.PATTERN_MONTH));
        }

        if (staffFilesVO.getChangeTime() != null) {
            staffFilesVO.setChangeTimeStr(DateUtil.format(staffFilesVO.getChangeTime(), DateUtil.PATTERN_MONTH));
        }

        if (staffFilesVO.getChangeDateTime() != null) {
            staffFilesVO.setChangeDateTimeStr(DateUtil.format(staffFilesVO.getChangeDateTime(), DateUtil.PATTERN_MONTH));
        }

        if (staffFilesVO.getApprovedTime() != null) {
            staffFilesVO.setApprovedTimeStr(DateUtil.format(staffFilesVO.getApprovedTime(), DateUtil.PATTERN_MONTH));
        }
        //单独处理数字宁波同步部门信息
        if (StringUtil.isNotBlank(staffFiles.getDataSource()) && staffFiles.getDataSource().equals("3")) {
            staffFilesVO.setDepartmentName(StringUtils.isNotBlank(staffFiles.getDef1()) ? staffFiles.getDef1() : null);
        }
        if(StringUtil.isNotBlank(staffFilesVO.getBackYear())){
            staffFilesVO.setYear(staffFilesVO.getBackYear());
        }
        return staffFilesVO;
    }
    @Override
    public File createReceptFile(Long id,String year) {
        try {
            int lenth = 0;
            String remake = "  1、侨眷是指华侨（定居在国外的中国公民）、归侨（回国定居的华侨在国内的眷属。侨眷包括：华侨、归侨的配偶，父母，子女及其配偶，兄弟姐妹，祖父母、外祖父母，孙子女、外孙子女，以及同华侨、归侨有长期无偿援助养关系的其他亲属。定居是指中国公民已取得住在国连续5年（含5年）以上合法居留资格，并在国外居住、视同定居。中国留学生（包括公派和自费）在外学习期间、因公出国人员（包括劳务人员）在外工作期间，不被视为定居。\n" +
                    "  2、工作单位及职务栏请填写××市××县（市、区）××单位××部门××职务或干××工作（外省工作的请注明××省）。在大专院校读书的子女请填写××大学××学院(系) ××专业学生。在国（境）外就读的应注明××国家××大学××学院(系) ××专业学生。在海（境）外的家庭成员请填写××国家××城市××单位××部门干××工作。\n" +
                    "  3、父母亲已去世的需填写原所在单位部门及职务，并写明“（已去世）”。\n";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            StaffFilesOldVO bean = staffFilesOldWrapper.entityToVO(getById(id));
            bean = setChild(bean);
            lenth = bean.getEducationDegreeVO() == null ? 0 : bean.getEducationDegreeVO().size();
            String[][] Study = new String[lenth][3];
            for (int i = 0; i < lenth; i++) {
                Study[i][0] = bean.getEducationDegreeVO().get(i).getEducationalName();
                Study[i][1] = (bean.getEducationDegreeVO().get(i).getStartDate() == null ? "" : sdf.format(bean.getEducationDegreeVO().get(i).getStartDate())) + "--" +
                        (bean.getEducationDegreeVO().get(i).getEndDate() == null ? "" : sdf.format(bean.getEducationDegreeVO().get(i).getEndDate()));
                Study[i][2] = "";
                if (StringUtil.isNotBlank(bean.getEducationDegreeVO().get(i).getSchoolDepartmentMajor())) {
                    Study[i][2] += bean.getEducationDegreeVO().get(i).getSchoolDepartmentMajor();
                }
                if (StringUtil.isNotBlank(bean.getEducationDegreeVO().get(i).getDegreeName())) {
                    Study[i][2] += bean.getEducationDegreeVO().get(i).getDegreeName();
                }

            }
            String yearAssessment = "";
            lenth = bean.getAnnualAssessmentVO() == null ? 0 : bean.getAnnualAssessmentVO().size();
            for (int i = 0; i < lenth; i++) {
                if (StringUtil.isNotBlank(bean.getAnnualAssessmentVO().get(i).getAssessmentYear()) && StringUtil.isNotBlank(bean.getAnnualAssessmentVO().get(i).getAssessmentResult())) {
                    yearAssessment += bean.getAnnualAssessmentVO().get(i).getAssessmentYear() + "年考核" + bean.getAnnualAssessmentVO().get(i).getAssessmentResult() + ";";
                }
            }
            lenth = bean.getWorkExperienceVO() == null ? 0 : bean.getWorkExperienceVO().size();
            String[] resume = new String[lenth];
            for (int i = 0; i < lenth; i++) {
                resume[i] = (bean.getWorkExperienceVO().get(i).getStartDate() == null ? "" : sdf.format(bean.getWorkExperienceVO().get(i).getStartDate())) + "--" +
                        (bean.getWorkExperienceVO().get(i).getEndDate() == null ? "" : sdf.format(bean.getWorkExperienceVO().get(i).getEndDate())) + "   " +
                        bean.getWorkExperienceVO().get(i).getWorkUnit() + "   " + bean.getWorkExperienceVO().get(i).getJobNameId();
            }
            lenth = bean.getRewardsPunishmentsVO() == null ? 0 : bean.getRewardsPunishmentsVO().size();
            String[] rewardsPunishments = new String[lenth];
            for (int i = 0; i < lenth; i++) {
                rewardsPunishments[i] = bean.getRewardsPunishmentsVO().get(i).getRewardDate() == null ? "" : sdf.format(bean.getRewardsPunishmentsVO().get(i).getRewardDate()) + bean.getRewardsPunishmentsVO().get(i).getGrantingUnitName() + "  " +
                        bean.getRewardsPunishmentsVO().get(i).getRewardPunishmentLevel() + "  " + bean.getRewardsPunishmentsVO().get(i).getHonorAwardName();
            }
            lenth = bean.getTrainVO() == null ? 0 : bean.getTrainVO().size();
            String[][] Train = new String[lenth][2];
            for (int i = 0; i < lenth; i++) {
                Train[i][0] = (bean.getTrainVO().get(i).getStartTime() == null ? "" : sdf.format(bean.getTrainVO().get(i).getStartTime())) + "--" +
                        (bean.getTrainVO().get(i).getEndTime() == null ? "" : sdf.format(bean.getTrainVO().get(i).getEndTime()));
                Train[i][1] = bean.getTrainVO().get(i).getClasses();
            }
            lenth = bean.getSocialRelationsVO() == null ? 0 : bean.getSocialRelationsVO().size();
            String[][] socialRelations = new String[lenth][5];
            for (int i = 0; i < lenth; i++) {
                socialRelations[i][0] = bean.getSocialRelationsVO().get(i).getAppellation();
                socialRelations[i][1] = bean.getSocialRelationsVO().get(i).getFullName();
                socialRelations[i][2] = bean.getSocialRelationsVO().get(i).getBirthday() == null ? "" : sdf.format(bean.getSocialRelationsVO().get(i).getBirthday());
                socialRelations[i][3] = bean.getSocialRelationsVO().get(i).getPoliticCountenanceName();
                socialRelations[i][4] = bean.getSocialRelationsVO().get(i).getWorkUnit() + bean.getSocialRelationsVO().get(i).getJobNameId();
            }

            lenth = bean.getOverseasMembersVO() == null ? 0 : bean.getOverseasMembersVO().size();
            String[][] overseasMembers = new String[lenth][4];
            for (int i = 0; i < lenth; i++) {
                overseasMembers[i][0] = bean.getOverseasMembersVO().get(i).getOverseasName();
                overseasMembers[i][1] = bean.getOverseasMembersVO().get(i).getOverseasSexName();
                overseasMembers[i][2] = bean.getOverseasMembersVO().get(i).getHostCountry() + bean.getOverseasMembersVO().get(i).getOccupation();
                overseasMembers[i][3] = bean.getOverseasMembersVO().get(i).getRelationship();
            }


            Document document = new Document(com.itextpdf.text.PageSize.A4, 90, 83, 30, 40);
            FileOutputStream fs;
            BaseFont bfHeiTi;
            BaseFont bfST;
            BaseFont test;
            BaseFont bfbt;

            File pdffile = File.createTempFile(id.toString(), ".pdf");
            if (pdffile.exists()) {
                pdffile.delete();
            }
            fs = new FileOutputStream(pdffile.getPath());//getAbsolutePath
            PdfWriter.getInstance(document, fs);
            // 打开文档
            document.open();
            bfST = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            bfHeiTi = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            bfbt = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            test = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 创建了一页
            Font font_Zhengwen = new Font(bfST, 12.0f, Font.NORMAL, BaseColor.BLACK);
            // 添加标题
            Font font_Biaoti = new Font(bfHeiTi, 20.0f, Font.BOLD, BaseColor.BLACK);
            Font font_biaotou1 = new Font(test, 12.0f, Font.NORMAL, BaseColor.BLACK);
            Font font_biaotou2 = new Font(bfbt, 12.0f, Font.NORMAL, BaseColor.BLACK);
            // 创建一个段落
            String title = "个人基本情况表";
            Paragraph biaoti = new Paragraph(title, font_Biaoti);
            biaoti.setAlignment(1);
            biaoti.setSpacingAfter(20f);
            document.add(biaoti);
            float[] widths = {60f, 60f, 60f, 60f, 30f, 30f, 30f, 30f};// 设置表格的列宽
            PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
            table.setSpacingBefore(10f);// 设置表格上面空白宽度
            table.setSpacingAfter(0f);// 设置表格下面空白宽度
            table.setTotalWidth(420);// 设置表格的宽度
            PdfPCell cell = new PdfPCell();
            PdfPCell cell2 = new PdfPCell();
            cell.setMinimumHeight(25f);
            cell2.setMinimumHeight(25f);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setPaddingTop(5);
            cell.setPaddingBottom(5);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setPaddingTop(5);
            cell2.setPaddingBottom(5);
            table.setLockedWidth(true);

            //正式开始
            cell.setPhrase(new Paragraph("姓名", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getStaffName(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("出生年月", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getBirthday() == null ? "" : sdf.format(bean.getBirthday()), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("性别", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getGenderName(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("民族", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getNationName(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("籍贯", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setRowspan(1);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getNativePlace(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("入党时间", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getJoinPartyTime() == null ? "" : sdf.format(bean.getJoinPartyTime()), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("健康状况", font_biaotou2));
            cell.setColspan(2);
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getHealthConditionName(), font_Zhengwen));
            cell2.setColspan(2);
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("出生地", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getBirthPlace(), font_Zhengwen));
            cell2.setColspan(1);
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("参加工作时间", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getWorkTime() == null ? "" : bean.getWorkTime(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("熟悉专业\n有何专长", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            cell.setRowspan(2);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getExpertise(), font_Zhengwen));
            cell2.setColspan(2);
            cell2.setRowspan(2);
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("学历", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(1);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getEducationalName(), font_Zhengwen));
            cell2.setColspan(1);
            cell2.setRowspan(1);
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("学位或专业\n技术职称", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            if (StringUtil.isBlank(bean.getDegreeName())) {
                bean.setDegreeName("");
            }
            if (StringUtil.isBlank(bean.getTechnicalPosition())) {
                bean.setTechnicalPosition("");
            }
            cell2.setPhrase(new Paragraph(bean.getDegreeName() + bean.getTechnicalPosition(), font_Zhengwen));
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("现任职务", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getJobNameName(), font_Zhengwen));
            cell2.setColspan(7);
            table.addCell(cell2);

            cell.setPhrase(new Paragraph("岗位职责", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getResponsibilities(), font_Zhengwen));
            table.addCell(cell2);

            document.add(table);//第一组结束

            float[] widths2 = {25f, 35f, 90f, 210f};// 设置表格的列宽
            PdfPTable table2 = new PdfPTable(widths2);// 建立一个pdf表格
            table2.setSpacingBefore(0f);// 设置表格上面空白宽度
            table2.setSpacingAfter(0f);// 设置表格下面空白宽度
            table2.setTotalWidth(420);// 设置表格的宽度
            table2.setLockedWidth(true);

            cell.setPhrase(new Paragraph("历次\n学历\n学习\n情况", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(Study.length < 4 ? 5 : Study.length + 1);
            table2.addCell(cell);
            cell.setPhrase(new Paragraph("学历", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(1);
            table2.addCell(cell);
            cell.setPhrase(new Paragraph("起止时间", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table2.addCell(cell);
            cell.setPhrase(new Paragraph("院校、系、专业及学位", font_biaotou2));
            cell.setColspan(1);
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            lenth = Study.length < 4 ? 4 : Study.length;
            for (int i = 0; i < lenth; i++) {
                for (int j = 0; j < 3; j++) {
                    if (i < Study.length) {
                        cell2.setPhrase(new Paragraph(Study[i][j], font_Zhengwen));
                    } else {
                        cell2.setPhrase(new Paragraph("", font_Zhengwen));
                    }
                    cell2.setColspan(1);
                    cell2.setRowspan(1);
                    table2.addCell(cell2);
                }
            }

            cell.setPhrase(new Paragraph("年度\n考核", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(1);
            table2.addCell(cell);
            cell2.setPhrase(new Paragraph(yearAssessment, font_Zhengwen));
            cell2.setColspan(3);
            cell2.setRowspan(1);
            table2.addCell(cell2);

            cell.setPhrase(new Paragraph("简 \n\n\n\n\n\n历", font_biaotou1));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setMinimumHeight(250f);
            table2.addCell(cell);
            String jl = org.apache.commons.lang3.StringUtils.join(resume, "\n");
            if (StringUtil.isBlank(jl)) {
                jl = "";
            }
            cell2.setPhrase(new Paragraph(jl, font_Zhengwen));
            //cell2.setHorizontalAlignment(Element.ALIGN_TOP);
            cell2.setColspan(3);
            table2.addCell(cell2);

            document.add(table2);

            document.newPage();
            float[] widths21 = {25f, 35f, 120f, 180f};// 设置表格的列宽
            PdfPTable table21 = new PdfPTable(widths21);// 建立一个pdf表格
            table21.setSpacingBefore(0f);// 设置表格上面空白宽度
            table21.setSpacingAfter(0f);// 设置表格下面空白宽度
            table21.setTotalWidth(420);// 设置表格的宽度
            table21.setLockedWidth(true);

            cell.setPhrase(new Paragraph("奖惩\n情况", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(2);
            cell.setMinimumHeight(40f);
            table21.addCell(cell);
            String j2 = org.apache.commons.lang3.StringUtils.join(rewardsPunishments, "\n");
            cell2.setPhrase(new Paragraph(j2, font_Zhengwen));
            cell2.setColspan(3);
            cell2.setRowspan(2);
            table21.addCell(cell2);

            cell.setPhrase(new Paragraph("近五\n年三\n个月\n以上\n培训、\n学习\n情况", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(Train.length < 3 ? 4 : Train.length + 1);
            cell.setMinimumHeight(30f);
            table21.addCell(cell);
            cell.setPhrase(new Paragraph("起止时间", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            cell.setRowspan(1);
            table21.addCell(cell);
            cell.setPhrase(new Paragraph("班      次", font_biaotou2));
            cell.setColspan(1);
            cell.setRowspan(1);
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table21.addCell(cell);
            lenth = Train.length < 3 ? 3 : Study.length;
            for (int i = 0; i < lenth; i++) {
                for (int j = 0; j < 2; j++) {
                    if (i < Train.length) {
                        cell2.setPhrase(new Paragraph(Train[i][j], font_Zhengwen));
                    } else {
                        cell2.setPhrase(new Paragraph("", font_Zhengwen));
                    }
                    switch (j) {
                        case 0:
                            cell2.setColspan(2);
                            break;
                        case 1:
                            cell2.setColspan(1);
                            break;
                    }
                    cell2.setRowspan(1);
                    table21.addCell(cell2);
                }
            }
            document.add(table21);//第二组结束

            float[] widths3 = {25f, 55f, 50f, 50f, 50f, 130f};
            PdfPTable table3 = new PdfPTable(widths3);
            table3.setSpacingBefore(0f);
            table3.setSpacingAfter(0f);
            table3.setTotalWidth(420);
            table3.setLockedWidth(true);

            cell.setPhrase(new Paragraph("家庭\n主要\n成员\n及\n重要\n社会\n关系", font_biaotou1));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(socialRelations.length < 5 ? 6 : socialRelations.length + 1);
            table3.addCell(cell);
            cell.setPhrase(new Paragraph("称谓", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(1);
            table3.addCell(cell);
            cell.setPhrase(new Paragraph("姓名", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table3.addCell(cell);
            cell.setPhrase(new Paragraph("出生年月", font_biaotou2));
            cell.setColspan(1);
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table3.addCell(cell);
            cell.setPhrase(new Paragraph("政治面貌", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table3.addCell(cell);
            cell.setPhrase(new Paragraph("工作单位及职务", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table3.addCell(cell);
            lenth = socialRelations.length < 5 ? 5 : Study.length;
            for (int i = 0; i < lenth; i++) {
                for (int j = 0; j < 5; j++) {
                    if (i < socialRelations.length) {
                        cell2.setPhrase(new Paragraph(socialRelations[i][j], font_Zhengwen));
                    } else {
                        cell2.setPhrase(new Paragraph("", font_Zhengwen));
                    }
                    cell2.setRowspan(1);
                    table3.addCell(cell2);
                }
            }
            document.add(table3);

            float[] widths4 = {25f, 55f, 50f, 150f, 80f};
            PdfPTable table4 = new PdfPTable(widths4);
            table4.setSpacingBefore(0f);
            table4.setSpacingAfter(0f);
            table4.setTotalWidth(420);
            table4.setLockedWidth(true);

            cell.setPhrase(new Paragraph("侨眷\n情况", font_biaotou1));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(overseasMembers.length < 1 ? 2 : overseasMembers.length + 1);
            table4.addCell(cell);
            cell.setPhrase(new Paragraph("华侨、归侨\n姓名", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            cell.setRowspan(1);
            table4.addCell(cell);
            cell.setPhrase(new Paragraph("性别", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table4.addCell(cell);
            cell.setPhrase(new Paragraph("所在国家、城市、职业", font_biaotou2));
            cell.setColspan(1);
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table4.addCell(cell);
            cell.setPhrase(new Paragraph("与本人的\n眷属关系", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table4.addCell(cell);
            lenth = overseasMembers.length < 1 ? 1 : Study.length;
            for (int i = 0; i < lenth; i++) {
                for (int j = 0; j < 4; j++) {
                    if (i < overseasMembers.length) {
                        cell2.setPhrase(new Paragraph(overseasMembers[i][j], font_Zhengwen));
                    } else {
                        cell2.setPhrase(new Paragraph("", font_Zhengwen));
                    }
                    cell2.setColspan(1);
                    table4.addCell(cell2);
                }
            }

            cell.setPhrase(new Paragraph("本人身份证号", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table4.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getDocumentNumber(), font_Zhengwen));
            cell2.setColspan(3);
            table4.addCell(cell2);

            cell.setPhrase(new Paragraph("家庭地址", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table4.addCell(cell);
            cell2.setPhrase(new Paragraph(bean.getFamilyAddress(), font_Zhengwen));
            cell2.setColspan(3);
            table4.addCell(cell2);

            cell.setPhrase(new Paragraph("填\n报\n说\n明", font_biaotou2));
            //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(1);
            table4.addCell(cell);
            cell2.setPhrase(new Paragraph(remake, font_Zhengwen));
            cell2.setColspan(4);
            table4.addCell(cell2);
            document.add(table4);
            document.close();

            return pdffile;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
        return null;
    }
}